<template>
	<view class="view-root">
		<scroll-view class="content-scroll" scroll-y="true">
			<view class="scroll-flex-wrap">
				<view style="position: relative;">
					<swiper class="swiper-box" @change="picChange">
						<swiper-item v-for="(picUrl, index) in mDetail.bannerImageList" :key="index" style="display: flex;justify-content: center;">
							<image mode="heightFix" class="banner-bannerImg" :src="picUrl"></image>
						</swiper-item>
					</swiper>
					<view class="index-notice">{{currentPic}}/{{mDetail.bannerImageList.length}}</view>
				</view>
				<view class="base-info">
					<view class="price-line">
						<view class="current-price">￥{{mDetail.salesPrice}}</view>
						<view class="old-price">￥{{mDetail.originalPrice}}</view>
					</view>
					<view class="name-line">
						<view class="prescription-tag" v-if="mDetail.recipeFlag > 0">处方药</view>
						<view class="medicine-name">{{mDetail.name}}</view>
					</view>
					<view class="medicine-abstract">{{mDetail.explainItems.majorCure}}</view>
					<view class="sub-base">
						<image src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_store_location.png"></image>
						<text>{{mDetail.merchantName}}·{{mDetail.merchantAreaInfo}}</text>
					</view>
				</view>
				<view class="sale-info">
					<view class="info-line">
						<view class="info-title">规格</view>
						<view class="info-content">{{mDetail.explainItems.specification}}</view>
					</view>
					<view class="sub-sale">
						<image src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_circle_hook.png"></image>
						<text>快递发货</text>
						<image src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_circle_hook.png"></image>
						<text>到店自提</text>
					</view>
				</view>
				<view class="explain-info" @click="showExplain">
					<view class="info-title">说明</view>
					<view class="right-contents">
						<view class="center-content">
							<view style="display: flex;box-sizing: border-box;">
								<view class="red-content">【批准文号】</view>
								<view class="info-content single-line">{{mDetail.explainItems.approvalNumber}}</view>
							</view>
							<view style="display: flex;margin-top: 24rpx;">
								<view class="red-content">【生产企业】</view>
								<view class="info-content single-line">{{mDetail.explainItems.manufacturer}}</view>
							</view>
						</view>
						<image style="width: 34rpx;height: 34rpx;" src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_gray_right.png"></image>
					</view>
				</view>
				<view class="base-info" style="padding: 24rpx;">
					<view class="info-title" style="margin: 0 0 16rpx 24rpx;">详情</view>
					<rich-text :nodes="h5Detail"></rich-text>
				</view>
			</view>
			<view style="height: 40rpx;"></view>
		</scroll-view>
		<view class="bottom-panel">
			<view class="img-btn" @click="showOrHideQRCode()">
				<image src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_medicine_share.png"></image>
				<text>分享</text>
			</view>
			<view class="img-btn" @click="gotoRouter('/pages-mall/medicineCart/medicineCart')">
				<image src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_medicine_list_cart.png"></image>
				<text>购物车</text>
				<view v-if="cartCount > 0" class="cart-count">{{cartCount}}</view>
			</view>
			<view class="cart-add-btn" @click="addCart()">加入购物车</view>
			<view class="purchase-btn" @click="directBuy()">{{mDetail.recipeFlag > 0 ? '去预约': '立即购买'}}</view>
		</view>
		<view v-if="explainVisible" class="explain-detail">
			<view class="outer-area" @click="showExplain"></view>
			<view class="explain-panel">
				<view class="explain-title">药品说明</view>
				<view class="explain-notice">请仔细阅读药品说明书注意事项、禁忌等内容，按说明书或在药师指导下购买和使用，将药品放置于儿童无法触及的位置。</view>
				<view class="explain-line">
					<view class="explain-line-head">批准文号：</view>
					<view class="explain-line-content">{{mDetail.explainItems.approvalNumber}}</view>
				</view>
				<view class="explain-line">
					<view class="explain-line-head">通用名称：</view>
					<view class="explain-line-content">{{mDetail.explainItems.universalName}}</view>
				</view>
				<view class="explain-line">
					<view class="explain-line-head">生产厂家：</view>
					<view class="explain-line-content">{{mDetail.explainItems.manufacturer}}</view>
				</view>
				<view class="explain-line">
					<view class="explain-line-head">规格：</view>
					<view class="explain-line-content">{{mDetail.explainItems.specification}}</view>
				</view>
				<view class="explain-line">
					<view class="explain-line-head">适用人群：</view>
					<view class="explain-line-content">{{mDetail.explainItems.usersPerson}}</view>
				</view>
				<view class="explain-line">
					<view class="explain-line-head">中西药分类：</view>
					<view class="explain-line-content">{{mDetail.explainItems.categoryStr}}</view>
				</view>
				<view class="explain-line">
					<view class="explain-line-head">口服外用：</view>
					<view class="explain-line-content">{{mDetail.explainItems.usageStr}}</view>
				</view>
				<view class="explain-line">
					<view class="explain-line-head">功能主治：</view>
					<view class="explain-line-content">{{mDetail.explainItems.majorCure}}</view>
				</view>
				<view class="read-btn" @click="showExplain">确定</view>
			</view>
		</view>
		<view v-if="qrcodeShow" class="qrcode-root">
			<view class="qrcode-dialog">
				<image class="close-btn"
					src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_float_close.png"
					@click="showOrHideQRCode()"></image>
					<view class="poster-wrap">
						<l-painter ref="painter">
							<l-painter-view css="width: 568rpx; box-sizing:border-box; background: #ffffff; align-items: center; padding: 24rpx; border-radius: 24rpx;">
								<l-painter-image :src="mDetail.bannerImageList[0]" css="width: 480rpx; height: 332rpx; margin: 24rpx auto; border-radius: 24rpx;"/>
								<l-painter-text css="display: block; font-size: 32rpx; color: #666666; margin: 24rpx 0 8rpx" :text="mDetail.name"></l-painter-text>
								<l-painter-text css="display: block; font-size: 36rpx; font-weight: 600; color: #FC4E4C; margin-bottom: 8rpx" :text="'￥' + mDetail.salesPrice"></l-painter-text>
								<l-painter-text css="display: block; font-size: 28rpx; color: #CCCCCC; margin-bottom: 16rpx" :text="mDetail.explainItems.majorCure"></l-painter-text>
								<l-painter-view css="display:block; height: 2rpx; background: #E5E5E5; margin: 0;"></l-painter-view>
								<l-painter-view css="display:flex; align-items:center; margin: 24rpx;">
									<l-painter-image css="width: 88rpx; height: 88rpx; border-radius: 44rpx; margin-right: 16rpx" :src="userInfo.avatar"></l-painter-image>
									<l-painter-view css="flex:1; box-sizing:border-box;">
										<l-painter-text css="font-size: 32rpx; color: #222222; font-weight: 500; display:block;" :text="userInfo.username"></l-painter-text>
										<l-painter-text css="font-size: 24rpx; color: #999999;" text="长按识别二维码"></l-painter-text>
									</l-painter-view>
									<l-painter-qrcode :text="qrcodeContent" css="width: 120rpx;height: 120rpx;"></l-painter-qrcode>
								</l-painter-view>
							</l-painter-view>
						</l-painter>
					</view>
					<view class="btn-line">
						<button class="confirm-btn" open-type="share">微信分享</button>
						<view class="confirm-btn" @click="saveQRCode()">保存至相册</view>
					</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
			userInfo
		} from '@/utils/token.js';
	export default {
		data() {
			return {
				userInfo: null,
				mDetail: {},
				currentPic: 1,
				h5Detail: '',
				explainVisible: false,
				cartCount: 0,
				qrcodeContent: '',
				qrcodeShow: false
			};
		},
		onLoad(params) {
			this.userInfo = userInfo.get();
			this.getDetail(params.id);
			this.qrcodeContent = 'https://disugroup.com/qrcode?scene=' + 'medicine_share&id=' + params.id;
		},
		onShow() {
			this.getCartCount();
		},
		onShareAppMessage(res) {
			return {
				title: this.mDetail.name,
				path: '/pages-mall/medicineDetail/medicineDetail?id=' + this.mDetail.id,
				imageUrl: this.mDetail.bannerImageList[0]
			}
		},
		methods: {
			getDetail(medicineId) {
				this.$api.getMedicineDetail({
					params: {
						id: medicineId
					}
				}).then(res => {
					if (res.code == 200) {
						this.mDetail = res.result;
						this.h5Detail = this.formatContent(res.result.description);
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				})
			},
			picChange(e) {
				this.currentPic = e.detail.current + 1;
			},
			showExplain() {
				this.explainVisible = !this.explainVisible;
			},
			getCartCount() {
				this.$api.getCartCount({}).then(res => {
					if (res.code == 200) {
						this.cartCount = res.result;
					} else {
						
					}
				})
			},
			addCart() {
				this.$api.addIntoCart({
					data: {
						commodityId: this.mDetail.id,
						count: 1
					}
				}).then(res => {
					if (res.code == 200) {
						this.cartCount = this.cartCount + 1;
						uni.showToast({
							title:"加入购物车成功",
							icon:'success'
						})
					} else {
						uni.showToast({
							title:res.message,
							icon:'none'
						})
					}
				})
			},
			directBuy() {
				if (this.mDetail.recipeFlag > 0) {
					this.gotoRouter('/pages-inquiry/inquirySelect/inquirySelect?type=2')
				} else {
					uni.navigateTo({
						url:'/pages-mall/orderCreate/orderCreate?medicines=' + JSON.stringify([
							{
							      "id" : this.mDetail.id,
							      "name" : this.mDetail.name,
							      "recipeFlag" : this.mDetail.recipeFlag,
							      "specification" : this.mDetail.explainItems.specification,
							      "count" : 1
							    }
						])
					})
				}
			},
			showOrHideQRCode() {
				this.qrcodeShow = !this.qrcodeShow;
			},
			
			saveQRCode() {
				uni.showLoading({
					title: '保存中',
					mask: true
				});
				this.$refs.painter.canvasToTempFilePath({
				  fileType: "jpg",
				  // 如果返回的是base64是无法使用 saveImageToPhotosAlbum，需要设置 pathType为url
				  pathType: 'url',
				  quality: 1,
				  success: (res) => {
					uni.showToast({
						icon: 'success',
						title: '保存成功'
					});
				    console.log(res.tempFilePath);
				    // 非H5 保存到相册
				    // H5 提示用户长按图另存
				    uni.saveImageToPhotosAlbum({
				        filePath: res.tempFilePath,
				        success: function () {
				            console.log('save success');
				        }
				    });
				  },
				  complete: () => {
					  uni.hideLoading();
				  }
				});
				
			},
			// 实现rich-text富文本中图片宽度最大100%
			formatContent(html) { 
				// 去掉img标签里的style、width、height属性
			 let content_data= html.replace(/<img[^>]*>/gi,function(match,capture){
			  match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
			  match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
			  match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
				return match;
			 }); 
			 // 修改所有style里的width属性为max-width:100%
			 content_data = content_data.replace(/style="[^"]+"/gi,function(match,capture){
			  match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
				return match;
			 }); 
			 // 去掉<br/>标签
			 content_data = content_data.replace(/<br[^>]*\/>/gi, ''); // img标签添加style属性：max-width:100%;height:auto
			 content_data = content_data.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0px auto;"');
			  return content_data;
			}
			
		}
	}
</script>

<style lang="less" scoped>
@import './medicineDetail.less';
</style>
